/* Copyright (c) 2007 Perimeter Financial Corp. All Rights Reserved.
 *
 * The materials contained in this document contain trade secrets and
 * confidential information of Perimeter Financial Corp. These materials
 * are not to be distributed, in whole or in part, without the prior 
 * written consent of Perimeter Financial Corp. Any right to use these 
 * materials or to include these materials in any other works must be 
 * specifically authorized by Perimeter Financial Corp.
 */
package com.lz.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lz.form.UploadItemDTO;
import com.lz.util.SQLConfig;

public class ItemDAO extends TheaDAO
{

    //Related to Gallery Maintain
    public void uploadGallery(UploadItemDTO item) {
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(SQLConfig.getQuery("UPLOADGALLERY"));

            ps.setString(1, item.getShopid());
            ps.setString(2, item.getImageId());
            ps.setString(3, item.getDescription());
            
            ps.setString(4, item.getIsbn());
            ps.setString(5, item.getPublisher() );
            ps.setString(6, item.getAuthor() );
            ps.setString(7, item.getYear() );
            ps.setString(8, item.getEdition() );
            ps.setString(9, item.getPrice() );
            ps.setString(10, item.getName() );
            ps.setString(11, item.getCategoryid());

            ps.executeUpdate();
        } catch (SQLException sqlex) {
            sqlex.printStackTrace();
            throw new RuntimeException(sqlex);
        } finally {
            closeConnection();
        }
    }
    
    public UploadItemDTO getItemDetails (UploadItemDTO item)
    {
        PreparedStatement ps = null;
        
        
        try {
            ps = conn.prepareStatement(SQLConfig.getQuery("GET_ITEM_DESC"));

            ps.setString(1, item.getShopid() );
            ps.setString(2, item.getImageId());
            
            ResultSet rs =  ps.executeQuery();
            if(rs.next())
            {
            	item.setDescription(rs.getString("description"));
            	   item.setIsbn(rs.getString("isbn"));
                   item.setPrice((rs.getString("price")));
                   item.setAuthor(rs.getString("author"));
                   item.setPublisher(rs.getString("publisher"));
                   item.setYear(rs.getString("year"));
                   item.setName(rs.getString("name"));
                   item.setEdition(rs.getString("edition"));
            }
            
        } catch (SQLException sqlex) {
            sqlex.printStackTrace();
            throw new RuntimeException(sqlex);
        } finally {
            closeConnection();
        }
        
        return item;
    }
    
    public String updateItemDetails (UploadItemDTO item)
    {
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(SQLConfig.getQuery("UPDATE_ITEM_DESC"));

            ps.setString(1, item.getDescription() );
            ps.setString(2, item.getIsbn());
            ps.setString(3, item.getPublisher() );
            ps.setString(4, item.getAuthor() );
            ps.setString(5, item.getYear() );
            ps.setString(6, item.getEdition() );
            ps.setString(7, item.getPrice() );
            ps.setString(8, item.getName() );
            
            
           
            
            ps.setString(9, item.getShopid() );
            ps.setString(10, item.getImageId() );
            
            ps.executeUpdate();
            
        } catch (SQLException sqlex) {
            sqlex.printStackTrace();
            throw new RuntimeException(sqlex);
        } finally {
            closeConnection();
        }
        
        return item.getDescription();
    }

    public void deleteItem (String shopId, String imageId)
    {
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(SQLConfig.getQuery("DELETE_ITEM"));

            ps.setString(1, shopId );
            ps.setString(2, imageId );
            
            ps.executeUpdate();
            
        } catch (SQLException sqlex) {
            sqlex.printStackTrace();
            throw new RuntimeException(sqlex);
        } finally {
            closeConnection();
        }
        
    }
    
    public ArrayList getItemList(String shopId)
    {
        PreparedStatement ps = null;
        ArrayList list =new ArrayList();
        try {
            ps = conn.prepareStatement(SQLConfig.getQuery("GET_ITEM_LIST"));

            ps.setString(1, shopId );
            ResultSet rs =  ps.executeQuery();
            while(rs.next())
            {
                list.add(rs.getString("imageid"));
            }
            
        } catch (SQLException sqlex) {
            sqlex.printStackTrace();
            throw new RuntimeException(sqlex);
        } finally {
            closeConnection();
        }
        
        return list;
    }
    
    public List getGalleryInfo(String shopId)
    {
        PreparedStatement ps = null;
        List gallery =new ArrayList();
        try {
            ps = conn.prepareStatement("select * from item where shopid=?");

            ps.setString(1, shopId );
            ResultSet rs =  ps.executeQuery();
            while(rs.next())
            {
        	UploadItemDTO image=new UploadItemDTO();
                image.setShopid(shopId);
                image.setImageId(rs.getString("imageid"));
                image.setDescription(rs.getString("description"));
                image.setIsbn(rs.getString("isbn"));
                image.setPrice((rs.getString("price")));
                image.setAuthor(rs.getString("author"));
                image.setPublisher(rs.getString("publisher"));
                image.setYear(rs.getString("year"));
                image.setName(rs.getString("name"));
                image.setEdition(rs.getString("edition"));
                
                gallery.add(image);
            }
            
        } catch (SQLException sqlex) {
            sqlex.printStackTrace();
            throw new RuntimeException(sqlex);
        } finally {
            closeConnection();
        }
        
        return gallery;
    }

}
